"""
2017年3月6日 星期一
算法学习 - 队列 queue
版本：  V3.5.1
"""
import  random
# 基于数组的队列实现 类
class Queue:
    # 队里生成 >> 长度，起始值，结束在
    def create(self,len=10,start=1,end=10):
        queueArr = []
        for i in range(1,len + 1):
            queueArr.append(random.randint(start,end))
        self._queue = queueArr
        return queueArr

    # 手动重置队列
    def reset(self,queueArr):
        self._queue = queueArr

    # 队列内插入数据
    def inertItem(self,value="random",index="last"):
        if (type(value) == str and value == "random"):
            value = random.randint(1,9999)
        queue = self._queue
        if (type(index) == str and index == "last") or type(index) != int:
            index = len(queue) - 1
        else:
            index = index - 1
        newQueue = []
        for k in range(len(queue)):
            if k == index:
                newQueue.append(value)
                newQueue.append(queue[k])
                continue
            newQueue.append(queue[k])
        self._queue = newQueue
        return newQueue[index]
    # 删除节点
    def deleteItem(self,index="last"):
        if type(index) == str or type(index) != int:
            index = len(self._queue) - 1
        else:
            index = index - 1
        newQueue = []
        for k in range(len(self._queue)):
            if k == index:
                continue
            newQueue.append(self._queue[k])
        self._queue = newQueue
        return newQueue

    # 返回队列
    def getQueue(self):
        return self._queue

def callQueueTest():
    # 类型调用
    q = Queue()
    print(q.create())
    q.inertItem("杨",5)
    q.inertItem("juan",8)
    q.inertItem()
    print(q.getQueue())
    q.deleteItem(8)
    print(q.getQueue())

# 严格队列的实现
class strictQueue:
    def __init__(self,size):
        self.size = size        # 队列大小
        self.point = -1         # 当前的指针，指向其实位置
        self._queue = []        # 队列的数据为列表
        self.isEmpty = True     # 是否为空队列
        self.isFull = False     # 是否为满队列

    def enQueue(self,value="random"):    # 进入队列
        if type(value) == str and value == "random":
            value = random.randint(1,999999)
        elif type(value) == list and len(value) != 0:
            for k in range(len(value)):
                self.enQueue(value[k])
            return
        point = self.point + 1
        curQueue = self._queue
        if point < len(curQueue):
            newQueue = []
            for k in range(len(curQueue)):
                if k == 0:      # 首页移出
                    continue
                newQueue.append(curQueue[k])
            self._queue = newQueue
        self._queue.append(value)
        # 队列状态标注
        if point == (self.size - 1):
            self.isFull = True
        else:
            self.isFull = False
        self.isEmpty = False
        if point == self.size - 1: # 如果队列到了最后面则重新开始
            point = -1
        self.point = point

    def deQueue(self):      # 出队列
        point = self.point
        newQueue = []
        curQurue = self._queue
        value = False
        for k in range(len(curQurue)):
            if k == point:
                self.point = self.point - 1
                value = curQurue[k]
                continue
            newQueue.append(curQurue[k])
        self.isFull = False
        if len(curQurue) == 0:
            self.isEmpty = True
        else:
            self.isEmpty = False
        self._queue = newQueue
        return value

    # 返回队列
    def getQueue(self):
        return self._queue
    # 将队列拼接为字符串
    def queue2Str(self,delimiter=''):
        q = self._queue
        content = ""
        try:
            content = delimiter.join(q)
        except:
            newArr = []
            for k in range(len(q)):
                newArr.append(str(q[k]))
            content = delimiter.join(newArr)
        return content

    def getStatus(self):
        return {"size":self.size,"point":self.point,"_queue":self._queue,"isEmpty":self.isEmpty,"isFull":self.isFull,'len_queue':len(self._queue)}

def callStrickQueue():
    q = strictQueue(5)

    # 单步入队列测试
    # q.enQueue()
    # print(q.getStatus())

    q.enQueue([1,2,3,4,5])
    print(q.getStatus())

    q.enQueue()
    print(q.getStatus())

    q.enQueue()
    print(q.getStatus())

###################### ###################### ###################### ###################### ######################

# callQueueTest()
callStrickQueue()